package com.lsl.legcommune.ui;

import com.lsl.legcommune.ui.helper.FragmentTabPagerAdapterManager;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class NetCommuneFragment extends Fragment {
	
	private FragmentActivity activityContext;
	
	private TabHost tabHost;
	private ViewPager pager;
	private FragmentTabPagerAdapterManager tabPagerAdapterManager;
	
	private RadioGroup radioGroup;
	
	private static final String TABTAG_NETCOMMUNE_CHINA = "TABTAG_NETCOMMUNE_CHINA";
	private static final String TABTAG_NETCOMMUNE_JPANESE = "TABTAG_NETCOMMUNE_JPANESE";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		activityContext = getActivity();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.netcommune,container,false);
		
		tabHost = (TabHost)v.findViewById(android.R.id.tabhost);
		tabHost.setup();
		
		pager = (ViewPager)v.findViewById(R.id.pager);
		
		radioGroup = (RadioGroup)v.findViewById(R.id.netcommune_radiogroup);
		
		return v;
	}
	
	
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {		
		super.onActivityCreated(savedInstanceState);
				
		tabPagerAdapterManager = new FragmentTabPagerAdapterManager(getChildFragmentManager(),activityContext,tabHost,pager);				
		
		tabPagerAdapterManager.addTab(tabHost.newTabSpec(TABTAG_NETCOMMUNE_CHINA).setIndicator(TABTAG_NETCOMMUNE_CHINA),NearestNetCommuneFragment.class,null,(RadioButton)radioGroup.findViewById(R.id.netcommune_radio_1));
		tabPagerAdapterManager.addTab(tabHost.newTabSpec(TABTAG_NETCOMMUNE_JPANESE).setIndicator(TABTAG_NETCOMMUNE_JPANESE),NearestNetCommuneFragment2.class,null,(RadioButton)radioGroup.findViewById(R.id.netcommune_radio_2));
		
		if (savedInstanceState != null) {
            tabHost.setCurrentTabByTag(savedInstanceState.getString("tabflag"));
        }
		
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {				
				switch(checkedId){
					case R.id.netcommune_radio_1:
						tabHost.setCurrentTabByTag(TABTAG_NETCOMMUNE_CHINA);
						break;
					case R.id.netcommune_radio_2:
						tabHost.setCurrentTabByTag(TABTAG_NETCOMMUNE_JPANESE);
						break;
					default :
						tabHost.setCurrentTabByTag(TABTAG_NETCOMMUNE_CHINA);
						break;						
				}
			}
		});
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {		
		super.onSaveInstanceState(outState);
		outState.putString("tabflag", tabHost.getCurrentTabTag());
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	
	
}
